package com.mysite.seckill.web.service;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.mysite.seckill.commons.exception.BusinessError;
import com.mysite.seckill.commons.exception.BusinessException;
import com.mysite.seckill.commons.po.User;
import com.mysite.seckill.web.mapper.UserMapper;
import com.mysite.seckill.web.util.CipherUtil;

@Service
public class UserService {

    protected final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private UserMapper userMapper;

    public User login(String username, String password) {
        User user = userMapper.login(username, CipherUtil.encode(password));
        if (user == null) {
            throw new BusinessException(BusinessError.USER_USERNAME_OR_PASSWORD_ERROR);
        }
        return user;
    }

    public boolean addUser(User user) {
        user.setPassword(CipherUtil.encode(user.getPassword()));
        userMapper.insertUser(user);
        return true;
    }
}
